﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Netstars.SessionCache;

namespace SessionCacheWeb
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
     
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable dt = SessionCache.GetInstance("{FC137707-0448-4FB5-8739-5B28142B6268}").GetValue<DataTable>("aaa");
            if (dt == null)
            {
                dt = new DataTable();
                dt.Columns.Add("list1",typeof(string));

                DataRow row = dt.NewRow();
                row.SetField("list1", TextBox1.Text);
                dt.Rows.Add(row);
                CacheDependency dependency = new CacheDependency();
                dependency.Add(AppDomain.CurrentDomain.BaseDirectory + "abc.txt");
                SessionCache.GetInstance("{FC137707-0448-4FB5-8739-5B28142B6268}")
                    .Insert("aaa", dt, dependency, SessionCache.NoAbsoluteExpiration, TimeSpan.FromSeconds(10), OnUpdateCallback);
                Label1.Text = "";
            }
            else
            {
                Label1.Text = dt.Rows[0][0] + "";
            }
        }

        private void OnUpdateCallback(string key, CacheItemRemovedReason reason, object expensiveObject, CacheDependency dependency)
        {
            string aaa = expensiveObject + "";
        }

        private void OnRemoveCallback(string key, object value, CacheItemRemovedReason reason)
        {
            //throw new Exception("ddd000");
           // Label2.Text = value + "ff";
        }

        protected void Button2_OnClick(object sender, EventArgs e)
        {
            SessionCache.GetInstance("{FC137707-0448-4FB5-8739-5B28142B6268}").Remove("aaa");
            Label1.Text = "移除成功!";

        }
    }
}